package cn.com.miao.lynx.plugin.push.service.impl;

import cn.com.miao.lynx.common.core.excpetion.LynxException;
import cn.com.miao.lynx.plugin.push.aop.holder.MessagePushContextHolder;
import cn.com.miao.lynx.plugin.push.model.MessagePushBody;
import cn.com.miao.lynx.plugin.push.model.PushClientConfig;
import cn.com.miao.lynx.plugin.push.service.MessagePushService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * AbstractMessagePushServiceImpl
 *
 * @title: AbstractMessagePushServiceImpl
 * @description:
 * @author: dengmiao
 * @create: 2020-01-14 14:10
 **/
@Slf4j
@AllArgsConstructor
public abstract class AbstractMessagePushServiceImpl implements MessagePushService {

    /**
     * default client name
     */
    private static final String DEFAULT_CLIENT_NAME = "default";
    /**
     * config push clients
     */
    private final Map<String, PushClientConfig> pushClientConfigs;
    /**
     * is production
     */
    @Getter
    private final boolean production;

    @Override
    public String getCurrentPushClientName() throws LynxException {
        String clientName = MessagePushContextHolder.get();
        log.debug("Use [{}] push message client", clientName);
        // if client name is null
        // use default client name
        return StringUtils.isEmpty(clientName) ? DEFAULT_CLIENT_NAME : clientName;
    }

    @Override
    public PushClientConfig getCurrentPushClient() throws LynxException {
        String currentClientName = getCurrentPushClientName();
        return pushClientConfigs.get(currentClientName);
    }
}
